import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sp_util/sp_util.dart';
import 'package:yp/helper.dart';
import 'package:yp/services/api.dart';
import '../../router.dart';
import 'state.dart';

class MySubscriptionController extends GetxController {
  final state = MySubscriptionState();
  @override
  void onInit() {
    super.onInit();
    var params = {
      'usr_num_id': SpUtil.getInt('empeNumId'),
    };
    Api.getCollect(params).listen((event) {
      print('订阅信息event---------$event');
      if (event['code'] == 0) {
        state.subList = event['content_info_list'];
      } else {
        state.subList = [];
      }
      update();
    });
  }

  @override
  void onReady() {
    // TODO: implement onReady
    super.onReady();
  }

  // ignore: slash_for_doc_comments
  /**
  * David
  * 购物，内容，tab切换
  */
  void tabSelect(BuildContext context) {
    state.shoppingSelect = !state.shoppingSelect;
    update();
  }

  // ignore: slash_for_doc_comments
  /**
  * David
  * 获取订阅内容
  */
  void getCollect(BuildContext context) {
    var params = {
      'usr_num_id': SpUtil.getInt('empeNumId'),
    };
    Api.getCollect(params).listen((event) {
      if (event['code'] == 0) {
        state.subList = event['content_info_list'];
        print('state.subList---------${state.subList}');
      } else {}
    });
    update();
  }

  // ignore: slash_for_doc_comments
  /**
  * David
  * 取消订阅内容
  */
  void canleCollectInfo(BuildContext context, int series) {
    var params = {
      'content_series': series,
      'flag': -1,
      'usr_num_id': SpUtil.getInt('empeNumId'),
    };

    Api.cancleCollect(params).listen(
      (event) {
        print('eventtttttttt==========$event');
        if (event['code'] == 0) {
          toast('取消成功');
          var params = {
            'usr_num_id': SpUtil.getInt('empeNumId'),
          };
          Api.getCollect(params).listen((event) {
            print('even2222222==========$event');
            if (event['code'] == 0 && event['content_info_list'] != null) {
              state.subList = event['content_info_list'];
            } else {
              state.subList = [];
            }
            update();
          });
        }
      },
    );
    update();
  }

  // ignore: slash_for_doc_comments
  /**
  * David
  * 跳转订阅详情
  */
  void getDetail(BuildContext context, Map info) {
    Get.toNamed(RouteConfig.eventDetail, arguments: {
      'is_like': info['is_like'], //是否点赞
      'likes_num': info['likes_num'], //
      'view_num': info['view_num'],
      'is_collection': info['is_collection'], //点赞数量
      'series': info['series']
    });
  }
}
